import { Node, AudioSource, AudioClip, resources, director, CCClass } from 'cc';
import { ResMgr } from './ResMgr';

export class Sound {
    private static _inst: Sound;
    public static get inst(): Sound {
        if (this._inst == null) {
            this._inst = new Sound();
        }
        return this._inst;
    }

    public get SoundSource() : AudioSource{
        return this._soundSource;
    }
    private _soundSource: AudioSource;

    public get BgmSource() : AudioSource{
        return this._bgmSource;
    }
    private _bgmSource : AudioSource;


    public get SoundVolume() { return this._soundVolume; }
    public set SoundVolume(value : number) { 
        this._soundVolume = value;
        this._soundSource.volume = this._soundVolume;
     }

    public get BGMVolume() { return this._bgmVolume; }
    public set BGMVolume(value : number) { 
        this._bgmVolume = value;
        this.BgmSource.volume = this._bgmVolume;
    }

    private _soundVolume : number = 0.5;
    private _bgmVolume : number = 0.5;

    constructor() {
        let audioMgr = new Node();
        audioMgr.name = '_Sound';
        director.getScene().addChild(audioMgr);
        director.addPersistRootNode(audioMgr);
        this._soundSource = audioMgr.addComponent(AudioSource);
        this._bgmSource = audioMgr.addComponent(AudioSource);
    }


    public OneShot(sound: AudioClip | string, volume?: number) {
        if (sound instanceof AudioClip) {
            this._soundSource.playOneShot(sound, volume ? volume : this.SoundVolume);
        }
        else {
            ResMgr.inst.load(sound,AudioClip,null,(err, clip: AudioClip) => {
                if (err) {
                    console.log(err);
                }
                else {
                    this._soundSource.playOneShot(clip, volume ? volume : this.SoundVolume);
                }
            });
        }
    }
    public StopOnShot() {
        this._soundSource.stop();
    }
    public PauseOnShot() {
        this._soundSource.pause();
    }
    public ResumeOnShot(){
        this._soundSource.play();
    }


    public SetBGM(sound: AudioClip | string, volume?: number) {
        if (sound instanceof AudioClip) {
            this._bgmSource.stop();
            this._bgmSource.clip = sound;
            this._bgmSource.loop = true;
            this._bgmSource.play();
            this._bgmSource.volume = volume ? volume : this.BGMVolume;
        }
        else {
            ResMgr.inst.load(sound,AudioClip,null,(err, clip: AudioClip) => {
                if (err) {
                    console.log(err);
                }
                else {
                    this._bgmSource.stop();
                    this._bgmSource.clip = clip;
                    this._bgmSource.loop = true;
                    this._bgmSource.play();
                    this._bgmSource.volume = volume ? volume : this.BGMVolume;
                }
            });
        }
    }
    public StopBGM() {
        this._bgmSource.stop();
    }
    public PauseBGM() {
        this._bgmSource.pause();
    }
    public ResumeBGM(){
        this._bgmSource.play();
    }
}